package guoanmaker.operator.business.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorHotBusinessNewForAPP;
import guoanmaker.operator.business.model.ProviderBusiness;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.modelnobind.Businessinfo;
import guoanmaker.operator.business.modelnobind.HotBusinessVO;
import guoanmaker.operator.business.repository.OperatorHotBusinessNewForAPPRepository;

/**
 * 
    * @ClassName: OperatorHotBusinessNewService  
    * @Description: 新版运营后台热门服务商配置接口  
    * @author lsy  
    * @date 2017年12月14日 下午5:32:47    
    *
 */
@Service
public class OperatorHotBusinessNewService implements OperatorHotBusinessNewServiceInterface {
	@Autowired
	private OperatorHotBusinessNewForAPPRepository operatorHotBusinessNewForAPPRepository;

	@PersistenceContext
	private EntityManager manager;
	/**
	 * 
	    * @Title: saveOperatorHotBusinessNew  
	    * @Description: 保存 APP 热门商户 
	    * @param @param inRoundPhone InRoundPhone中的OperatorHotBusinessNewForAPP 数组 和cityCode
	    * @throws
	 */
	@Override
	@Transactional
	public void saveOperatorHotNewBusiness(InRoundPhone inRoundPhone) {
		OperatorHotBusinessNewForAPP[] operatorHotBusinessNews = inRoundPhone.getOperatorHotBusinessNewForAPPs();
		for (OperatorHotBusinessNewForAPP operatorHotBusinessNewForAPP : operatorHotBusinessNews) {
			operatorHotBusinessNewForAPP.setCityCode(inRoundPhone.getCityId());
			operatorHotBusinessNewForAPP.setCreateDate(new Date());
			operatorHotBusinessNewForAPPRepository.save(operatorHotBusinessNewForAPP);
		}
		
	}
	@Transactional
	public void delectAllBycitycode(String citycode) {
		String jpql = "delete  from OperatorHotBusinessNewForAPP  b where b.cityCode=:citycode";
		Query query2 = manager.createQuery(jpql);
		query2.setParameter("citycode", citycode);
		query2.executeUpdate();

	}
	
	
	/**
	 * @Title: selectOperatorBusiness  
	    * @Description: 传入服务商编号 连接用户表 查询服务商是否存在  
	    * @param  busNumber 服务商编号
	    * @return ProvideShopVo      
	    * @throws
	 */
	@Override
	public HotBusinessVO selectOperatorBusiness(String busNumber) {
		String jpql = "select new guoanmaker.operator.business.modelnobind.HotBusinessVO(b.id,p.companyLogo,b.businessName,b.companyIntroduce,p.followNum) from "+
		"ProvideShop p left join p.providerBusiness b inner join b.personalUser u where u.isProvider=2 and  b.busNumber =:busNumber";
		Query query = manager.createQuery(jpql);
		query.setParameter("busNumber", busNumber);
		List<HotBusinessVO>   hotBusinessVOs = query.getResultList();
		if (hotBusinessVOs != null && hotBusinessVOs.size() >0) {
			return hotBusinessVOs.get(0);
		}
		return null;
	}
	
	/**
	 * 
	    * @Title: findOperatorHotBusinessNew  
	    * @Description: 根据citycode 查询所有热门商户  
	    * @param @param cityCode
	    * @return AjaxJson      
	    * @throws
	 */
	@Override
	public List<OperatorHotBusinessNewForAPP> findOperatorHotBusinessNew(String cityCode) {
		return operatorHotBusinessNewForAPPRepository.findOperatorHotBusinessNewByCityCode(cityCode);
	}

}
